﻿using Sdk;
using System;
using UnityEngine;
using UnityEngine.UI;

namespace DiverseStudents.UI
{
    internal static partial class UICreate
    {

        public static RectTransform SettingRect(bool _show = true)
        {
            if (settingView != null)
            {
                var group_bg = settingView.FindComponent<RectTransform>("group_bg");
                if (group_bg != null && group_bg != default)
                {
                    var bg = GameObject.Instantiate(group_bg);
                    bg.name = "Mods_View";
                    bg.gameObject?.SetActive(_show);
                    ClearChilds(bg.transform);
                    return bg;
                }
            }
            var rect = CreateErrorRect<RectTransform>();
            rect.sizeDelta = new Vector2(1920, 1080);
            return rect;
        }

        public static RectTransform BeseRectBack(string flag, bool _show = true)
        {
            if (detailIntentView != null)
            {
                var gi = detailIntentView.FindComponent<RectTransform>("group_data/group_intent");
                if(gi != null && gi != default)
                {
                    var ngi = GameObject.Instantiate(gi);
                    ngi.name = $"Mod_{flag}RectBack";
                    ngi.pivot = new Vector2(0.5f, 0.5f);
                    ngi.gameObject?.SetActive(_show);
                    ClearChilds(ngi.transform);
                    return ngi;
                }
                else
                {
                    DLog.Warring("Not find BeseRectStyle1");
                }
            }
            var rect = CreateErrorRect<RectTransform>();
            rect.sizeDelta = new Vector2(1920, 1080);
            return rect;
        }

        public static RectTransform FinishRect(bool _show = true)
        {

            if (detailZhuangchangView != null)
            {
               var fi = detailZhuangchangView.FindComponent<RectTransform>("Cell_AchievementItem/finish");
             
                if(fi != null && fi != default)
                {
                    var nfi  = GameObject.Instantiate(fi);
                    nfi.name = "Mod_RectFinish";
                    nfi.gameObject?.SetActive(_show);   
                    return nfi;
                }
            }
            return CreateErrorRect<RectTransform>();
        }

        public static ScrollRect ScrollRect(string flag, bool _show = true)
        {
            try
            {
                if (detailIntentView != null)
                {
                    var sv = detailIntentView.FindComponent<ScrollRect>("group_data/Scroll View");
                    if (sv != null && sv != default)
                    {
                        var nsv = GameObject.Instantiate(sv);
                        if (nsv != null)
                        {
                            nsv.name = $"Mod_ScrolRect{flag}";
                            nsv.gameObject?.SetActive(_show);
                            if (nsv.transform != null)
                                ClearChilds(nsv.transform);
                            return nsv;
                        }

                    }
                }
            }
            catch (Exception e)
            {
                DLog.Error(e);
            }
            var sr = CreateErrorRect<ScrollRect>();
            var rect = sr.transform as RectTransform;
            rect.sizeDelta = new Vector2(1920, 1080);
            return sr;
        }


        public static ScrollRect BeseScrollRect(bool _show = true)
        {
            GameObject gameObject = new GameObject($"Mod_ListView", typeof(ScrollRect));
            gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(200f, 200f);
            GameObject gameObject2 = SubUIObject("Viewport",gameObject, typeof(Image), typeof(Mask));
            GameObject gameObject3 = SubUIObject("Content", gameObject2, typeof(RectTransform),typeof(VerticalLayoutGroup));
            GameObject gameObject4 = BaseScrollbar();
            gameObject4.name = "Scrollbar Vertical";
            gameObject4.transform.SetParent(gameObject.transform, worldPositionStays: false);
            SetLayerRecursively(gameObject4, gameObject.layer);
            gameObject4.GetComponent<Scrollbar>().SetDirection(Scrollbar.Direction.BottomToTop, includeRectLayouts: true);
            RectTransform component1 = gameObject4.GetComponent<RectTransform>();
            component1.anchorMin = Vector2.right;
            component1.anchorMax = Vector2.one;
            component1.pivot = Vector2.one;
            component1.sizeDelta = new Vector2(component1.sizeDelta.x, 0f);
            RectTransform component2 = gameObject2.GetComponent<RectTransform>();
            component2.anchorMin = Vector2.zero;
            component2.anchorMax = Vector2.one;
            component2.pivot = Vector2.up;
            RectTransform component3 = gameObject3.GetComponent<RectTransform>();
            ScrollRect component4 = gameObject.GetComponent<ScrollRect>();
            component4.content = component3;
            component4.viewport = component2;
            component4.verticalScrollbar = gameObject4.GetComponent<Scrollbar>();
            component4.verticalScrollbarVisibility = UnityEngine.UI.ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
            component4.horizontalScrollbarSpacing = -3f;
            component4.verticalScrollbarSpacing = -3f;
            gameObject2.GetComponent<Mask>().showMaskGraphic = false;
            gameObject2.GetComponent<Image>().type = Image.Type.Sliced;
            VerticalLayoutGroup component6 = gameObject3.GetComponent<VerticalLayoutGroup>();
            component6.childControlHeight =false;
            component6.childControlWidth = true;
            component6.childForceExpandHeight =false;
            component6.childForceExpandWidth = true;
            component6.childAlignment = TextAnchor.UpperCenter;
            return component4;
        }

        private static GameObject BaseScrollbar()
        {
            GameObject gameObject = new GameObject("Scrollbar", typeof(Image), typeof(Scrollbar));
            gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(160f, 20f);
            GameObject gameObject2 = SubUIObject("Sliding Area", gameObject, typeof(RectTransform));
            GameObject gameObject3 = SubUIObject("Handle", gameObject2, typeof(Image));
            Image component = gameObject.GetComponent<Image>();
            component.type = Image.Type.Sliced;
            component.color = new Color(0.5f, 0.5f, 0.5f, 0.2f);
            Image component2 = gameObject3.GetComponent<Image>();
            component2.type = Image.Type.Sliced;
            component2.color = new Color(0.5f, 0.5f, 0.5f, 0.4f);
            RectTransform component3 = gameObject2.GetComponent<RectTransform>();
            component3.sizeDelta = new Vector2(-20f, -20f);
            component3.anchorMin = Vector2.zero;
            component3.anchorMax = Vector2.one;
            RectTransform component4 = gameObject3.GetComponent<RectTransform>();
            component4.sizeDelta = new Vector2(20f, 20f);
            Scrollbar component5 = gameObject.GetComponent<Scrollbar>();
            component5.handleRect = component4;
            component5.targetGraphic = component2;
            SetDefaultColorTransitionValues(component5);
            return gameObject;
        }

        private static void SetDefaultColorTransitionValues(Selectable slider)
        {
            ColorBlock colors = slider.colors;
            colors.highlightedColor = new Color(0.882f, 0.882f, 0.882f);
            colors.pressedColor = new Color(0.698f, 0.698f, 0.698f);
            colors.disabledColor = new Color(0.521f, 0.521f, 0.521f);
        }


        private static void SetLayerRecursively(GameObject go, int layer)
        {
            go.layer = layer;
            Transform transform = go.transform;
            for (int i = 0; i < transform.childCount; i++)
            {
                SetLayerRecursively(transform.GetChild(i).gameObject, layer);
            }
        }

        private static T CreateErrorRect<T>(bool _show = true) where T : Component
        {
            GameObject @object = new GameObject($"Mod_ErrorRect{typeof(T).Name}", typeof(T), typeof(Image));
            var trs = @object.GetOrAddComponent<T>();
            var image = @object.GetOrAddComponent<Image>();
            image.color = Color.white;
            image.transform.SetParent(trs.transform, false);
            @object.SetActive(_show);
            return trs;
        }
    }
}
